package com.zhuoyue.biz.core.command.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.JiXieShouKongZhiQi0Faa0213CmdDeal;
import com.zhuoyue.biz.core.command.constant.KongTiaoMianBan0Fac0202CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 机械手控制器控制类
 *
 * @author Administrator
 */
public class KongTiaoMianBan0Fac0202 implements Device {

    @Override
    public String getCommand(String controlParam) {
        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";
        if (JiXieShouKongZhiQi0Faa0213CmdDeal.ControlType.CONTROL_TYPE_DIRECT.equals(controlType)) {
            cmd = getDirectControlCmd(map);
        }

        return cmd;
    }


    /**
     * 获取机械手直接控制命令
     *
     * @param map
     * @return
     */
    private String getDirectControlCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd12 = actionsMap.get("controlState");

        String cmd10To12 = actionsMap.get("childOdIndex") +
                cmd12;

        String cmd9 = CmdUtils.getDateLength(cmd10To12);

        String cmd3To12 = KongTiaoMianBan0Fac0202CmdDeal.DirectControl.CMD_3 +
                KongTiaoMianBan0Fac0202CmdDeal.DirectControl.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                KongTiaoMianBan0Fac0202CmdDeal.DirectControl.CMD_7 +
                KongTiaoMianBan0Fac0202CmdDeal.DirectControl.CMD_8 +
                cmd9 +
                cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = JiXieShouKongZhiQi0Faa0213CmdDeal.DirectControl.CMD_1 +
                cmd2 +
                cmd3To12 +
                cmd13 +
                JiXieShouKongZhiQi0Faa0213CmdDeal.DirectControl.CMD_14;

        return fullCmd;
    }

}
